<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<head>
		<%@ include file="/common/common-css.jsp" %>
		<style>
		#jquery-table td{cursor:pointer !important}
		.load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
		.load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
		</style>
	</head>
<body  class="no-skin">
<div  class="load_bg" style="display:none;" ></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>
<div class="page-content">
	<div class="page-header" style="margin-bottom:0px;padding-bottom:10px;">
			
	</div>
			
	<form id="queryForm">
	<div class="page-content-area">
		<div class="row">
			<div class="col-sm-12">
				<div class="widget-box" style="border:0px solid #ccc; border-top:none;border-bottom:none;">
					<div class="widget-header widget-header-blue widget-header-flat">
						<h4 class="widget-title">选择条件进行查询</h4>
						<div class="widget-toolbar">
							<a href="#" data-action="collapse">
								<i class="ace-icon fa fa-chevron-up"></i>
							</a>
						</div>
					</div>
					<div class=" widget-body">
						<div class="widget-main" >
						    <div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
								<label class="col-sm-4 control-label"  style="text-align:right"  for="userID">活动ID:</label>
								<div class="col-sm-8" style="padding-left:0;">
									<input type="text" name="activityid" style="width:100%;text-align:left;">
								</div>
							</div>	
							<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
								<label class="col-sm-4 control-label"  style="text-align:right"  for="userID">活动名称:</label>
								<div class="col-sm-8" style="padding-left:0;">
									<input type="text" name="name" style="width:100%;text-align:left;">
								</div>
							</div>								
							<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
								<label class="col-sm-4 control-label"  style="text-align:right"  for="isprize">状态:</label>
								<div class="col-sm-8" style="padding-left:0;">
									<select class="form-control" name="status">
									    <option value="">请选择</option>
									    <option value="1">上线中</option>
									    <option value="2">已下线</option>
									</select>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="col-md-12 center">
				<div class="clearfix form-actions">
				   <button class="btn btn-grey  no-border" type="button"
						name="btn-query" onclick="checkAll()" id="btn-query">
						全选
					</button>
				    &nbsp;&nbsp;&nbsp;
				    <button class="btn btn-grey  no-border" type="button"
						name="btn-query" onclick="showInvest(1,'status')" id="btn-query">
						批量上线
					</button>
				    &nbsp;&nbsp;&nbsp;
				    <button class="btn btn-grey  no-border" type="button"
						name="btn-query" onclick="showInvest(0,'status')" id="btn-query">
						批量下线
					</button>
				    &nbsp;&nbsp;&nbsp;
					<button class="btn btn-purple  no-border" type="button"
						name="btn-query" onclick="query_any()" id="btn-query">
						<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
						查询
					</button>
					&nbsp;&nbsp;&nbsp;
					<button class="btn btn-grey  no-border" type="reset"
						onclick="Javascript:doAddPro();">
						<i class="ace-icon fa fa-undo icon-on-right bigger-110"></i> 新增
					</button>
					&nbsp;&nbsp;&nbsp;
					<button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="delRedis()" id="btn-query">
						<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
						删除缓存
					</button>
					&nbsp;&nbsp;&nbsp;
					<button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="updateindex()" id="btn-query">
						<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
						保存
					</button>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<table id="jquery-table" class="table table-striped table-bordered table-hover">
				</table>
			</div>
		</div>
	</div>
	</form>
</div>

<%@ include file="/common/common-js.jsp" %>
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script type="text/javascript" src="${ctx}/ace/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
var souceList = null;
     //【查询】按钮Click事件
	  function query_any(){
		$(".load_bg").removeAttr("style");
		$(".load_img").removeAttr("style");
		if(jqTable != null){
			jqTable.fnDraw();
    	}else{
    		loaddata();
    	}
	  }
	  var jqTable = null;
	  var loaddata = function(){
	  	jQuery(function($) {
	  		jqTable = $('#jquery-table').dataTable({
	  		    //数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
	  		    "columnDefs": [
						{	  
							  "title":'',
							  "targets": [0],
							  "data": "",   //JSON返回属性
							  "bSortable": false,
							  "width": "5%",
							  "render": function(value, type, row) { // 返回自定义内容
								  return '<input type="checkbox" class="" id="select" name="select" row="3" data-id='+row.id+' value='+row.id+' style="height:100% !important;"/>';
							  } 
						},
					 {	  
						  "title":'排序',
						  "targets": [1],
						  "data": "",   //JSON返回属性
						  "bSortable": false,
						  "width": "5%",
						  "render": function(value, type, row) { // 返回自定义内容
							  return '<i class="glyphicon glyphiconfir glyphicon-chevron-up" data-tabindex="'+row.id+'"></i><br><i class="glyphicon glyphicon-chevron-down"></i>';
						  } 
					},
					{
	  		    		  "title":"活动ID",
	  		    		  "name":"id",
	  		    		  "data": "id",
	  		    	  	  "targets": [2],
	  		    	  	  "bSortable": false,
	   		    	  	  "width": "7%",
	  		    	},
	  		    	{
	  		    		  "title":"活动名称",
	  		    		  "name":"name",
	  		    		  "data": "name",
	  		    	  	  "targets": [3],
	  		    	  	  "bSortable": false,
	   		    	  	  "width": "7%",
	  		    	},
	  		    	{
	  		    		  "title":"活动icon",
	  		    		  "name":"logo",
	  		    		  "data": "logo",
	  		    	  	  "targets": [4], 
	  		    	  	  "bSortable": false,
	   		    	  	  "width": "8%",
	   		    	  		"render": function(value, type, row) { // 返回自定义内容
	  					      var star="<img src='"+value+"' style='height:60px;'/>";
	  						  return star;
	  					  },
	  		    	},
			    	{
			    		  "title":"顺序",
			    		  "name":"position",
			    		  "data":"position",
			    	  	  "targets": [5], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "5%",
	 		    	  	  "render": function(value, type, row) { // 返回自定义内容
							  return '<textarea class="form-control backup-name" row="2" data-id='+row.id+' style="width:90%;height:100% !important;resize:none;">'+value+'</textarea>';						 
						  },
	 		   
			    	},
			    	{
			    		  "title":"状态",
			    		  "name":"status",
			    		  "data":"status",
			    	  	  "targets": [6], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "5%",
	 		    	  	  "render": function(value, type, row) { // 返回自定义内容
							if(value==1){
								return "上线中";
							}else if(value==0){
								return "已下线";
							}else{
								return "未知";
							}
		    	  	  }
			    	},
			    	{
			    		  "title":"操作人",
			    		  "name":"operator",
			    		  "data":"operator",
			    	  	  "targets": [7], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "5%",
			    	},
			    	{
			    		  "title":"修改时间",
			    		  "name":"utime",
			    		  "data":"utime",
			    	  	  "targets": [8], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "5%",
			    	},
			    	{
			    		  "title":"备注",
			    		  "name":"remark",
			    		  "data":"remark",
			    	  	  "targets": [9], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "10%",
			    	},
			    	{		
			    		  "title":"操作",
			    		  "name":"id",
			    		  "data": "id",
			    	  	  "targets": [10], 
			    	  	  "bSortable": false,
	 		    	  	  "width": "8%",
	 		    	  	  "render": function(value, type, row) { // 返回自定义内容
	 		    	  		var text = "<button type='button' value='"+value+"' name='infoID' class='btn btn-primary btn-sm no-border infoID'>编辑</button>";
	 		    	  		text += "<button type='button' value='"+value+"' class='btn btn-primary btn-sm no-border' onclick='editComment(" + row.id + ");'>编辑评论</button>"
	 		    	  		if(row.status==0){
				   			  text += "  <a class='btn btn-success btn-sm' href='javascript:void(0)' onclick='updateStatus(" + row.id + ",1)'>上线</a>";
	 		    	  		}
	 		    	  		if(row.status==1){
				   			  text += "  <a class='btn btn-sm' href='javascript:void(0)' onclick='updateStatus(" + row.id + ",0)'>下线</a>";
	 		    	  		}
	 		    	  		return text;
		                  }
			    	},
	  		  	],
	  			"sAjaxSource": ctx+"/loanactivity/list.action",
	  			"fnServerData": function ( sSource, aoData, fnCallback ) {//从服务器请求数据
	  		   			var data = $('#queryForm').serializeObject();
	  					data.aoData = JSON.stringify(aoData);
	  					$.ajax({
	  						"dataType": 'json',
	  						"type": "POST",
	  						"url": sSource,
	  						"data":data,
	  						"success": function(resp){
	  							$(".load_bg").attr("style","display:none");
	  							$(".load_img").attr("style","display:none");
  								$("#hidden_pass").attr("style","display:block");
  								$('#jquery-table').removeClass("dataTable");
  								fnCallback(resp);
  								initListPage();
	  						},
	  						"error" : function(XMLHttpRequest,
	  								textStatus, errorThrown) {
	  							$(".load_bg").attr("style","display:none");
	  							$(".load_img").attr("style","display:none");
	  							alert('请求服务器错误,请刷新重试！！');//错误提示，包括服务器重启导致的session失效
	  	
	  						}
	  					});
	  			},
	  			"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
	  				//=====================这里实现行双击事件======================//
	  				$(nRow).on('click', 'td .infoID', function (index) {
	  					var id = $(this).val();
						var url = "/loanactivity/edit.action?id=" + id;
						url = encodeURI(url);
						openDialogWithRand(url,1000,1200,"",function(data){
							query_any();
						});
				     });
	  			},
	  			"bDestroy":true,
				"bScrollInfinite":true,
	  			"sScrollXInner": "100%",
	  			"sScrollY": "100%",//如果不设置，IE会有问题
	  		    "aaSorting": [[0,"desc"]] //初始化加载，默认按第几列排序、排序方式（列从0开始计算）
	  		});
	  	});	
	 }
	
	jQuery(function($) {
		//日期时间插件
		$('.date-picker').datetimepicker({
			  	language:  'zh-CN',
		        weekStart: 1,
		        todayBtn:  1,
				autoclose: 1,
				todayHighlight: 1,
				startView: 2,
				forceParse: 0,
				showMeridian: 1,
				format: 'yyyy-mm-dd hh:ii:ss'
		}).next().on(ace.click_event, function(){
			$(this).prev().focus();
		});
	});
	
	function editComment(activityId){
		var url = "/loanactivity/editComment.action?activityId="+activityId;
		url = encodeURI(url); 
		openDialogWithRand(url,1000,1200,"编辑评论",function (data){})
	}
	
	$(document).ready(function() {
		
		query_any();
		
		//上移
        $('#jquery-table').delegate('.glyphicon-chevron-up','click',function(){
			var arr = new Array();
			var thispos=$(this).parent('td').parent('tr');
			var thishtml=thispos.clone();
			var prevpos=$(this).parent('td').parent('tr').prev();
			var thisindex = thispos.index();
			if(thisindex==0){
				$('.discovery-up').show();
				setTimeout("$('.discovery-up').hide()",3000);
			}else{
				thispos.remove();
				prevpos.before(thishtml);
				$('#jquery-table tbody tr').each(function(){
					arr.push($(this).find('td:nth-child(2)').find('.glyphicon-chevron-up').attr('data-tabindex'));
				});
				var arrstring = arr.join("#");
				console.log(arrstring);
				 $.ajax({
					url: ctx+'/loanactivity/sortTab.action',
					type: 'POST',
					dataType: 'json',
					data: {"prdSortList": arrstring},
					success:function(data){
                        console.log(data);
					},
					error:function(){
						console.log('排序失败');
					}
				});
			  }
			});
	        //下移
			$('#jquery-table').delegate('.glyphicon-chevron-down','click',function(){
				var arr = new Array();
				var thispos=$(this).parent('td').parent('tr');
				var thishtml=thispos.clone();
				var nextpos=$(this).parent('td').parent('tr').next();
				var thisindex = thispos.index();
				if(thisindex==$("#jquery-table tbody tr").length - 1){
					$('.discovery-down').show();
					setTimeout("$('.discovery-down').hide()",3000);
				}else{
					thispos.remove();
			        nextpos.after(thishtml);
					$('#jquery-table tbody tr').each(function(){
						arr.push($(this).find('td:nth-child(2)').find('.glyphicon-chevron-up').attr('data-tabindex'));
					});
					var arrstring = arr.join("#");
					 $.ajax({
						url: ctx+'/loanactivity/sortTab.action',
						type: 'POST',
						dataType: 'json',
						data: {'prdSortList': arrstring},
						success:function(data){
	                        console.log(data);
						},
						error:function(){
							console.log('排序失败');
						}
					}); 
					
				}
			}); 
	});
	
	
	jQuery(function(){
		query_any();
		
		$('#jquery-table').delegate('textarea.backup-name','blur',function(){
			var _this = $(this);
			var id = _this.attr('data-id');
			var val = _this.val();
			var data = "id=" + id + "&position=" + val;
			$.ajax({
				"type": "POST",
				"dataType": 'json',
				"url": ctx + '/loanactivity/updatePosition.action',
				"data": data,
				"timeout":300000,
				"success": function(data){
					backupTextJustToolsTip(_this,'修改成功','tooltip-suc');
				},
				"error": function(){
					backupTextJustToolsTip(_this,'修改失败','tooltip-fail');
				}
			});			
			return false;
		});

	});
	
	function backupTextJustToolsTip(dom,content,theme){
		
		
		dom.justToolsTip({
			animation:"moveInLeft",
			contents:content,
			theme:theme,
			gravity:'left'
		});
		setTimeout(function(){
			$('.just-tooltip').remove();
		},3000); 
	};
	
	
	var flag = true;
	function delRedis(){
		if(flag){
			var url=ctx + "/loanactivity/delRedis.action";
			 $.ajax({
				"url":url ,
				"type": "POST",
				"dataType": 'json',
				"beforeSend": function(){
					flag = false;
				},
				"success": function(data){
					alert("操作成功");
					query_any();
					flag = true;
				},
				"error": function(){
					alert("操作失败");	
					flag = true;
				},
				"complete": function(){
					//解除事件
					flag = true;
				}
			});
		}
		
	}
	
	function showInvest(status){
		var ids ="";	
		$(":checkbox").each(function(){
			if($(this).is(':checked')){
				var id = $(this).val();
				ids += id+",";
			}
		});
		if(ids==""){
			return;
		}
		var url=ctx + "/loanactivity/showInvest.action?ids="+ids+"&status="+status;
		 $.ajax({
			"url":url ,
			"type": "POST",
			"dataType": 'json',
			"beforeSend": function(){
				flag = false;
			},
			"success": function(data){
				alert("操作成功");
				query_any();
				flag = true;
			},
			"error": function(){
				alert("操作失败");	
				flag = true;
			},
			"complete": function(){
				//解除事件
				flag = true;
			}
		});
	}
	
	function updateindex(){
		var valueindex = new Array();
		var ids = new Array();
		var flag =false;
		$("input[id='valueindex']").each(function(i,obj){
			var value = $(obj).val();
			var id = $(obj).attr("data-id");
			if(value==""){
				alert("顺序不能为空！");
				flag=true;
			}
			ids.push(id);
			valueindex.push(value);
		});
		if(flag){
			return;
		}
		 $.ajax({
			url: ctx+'/loanactivity/sortPosition.action',
			type: 'POST',
			dataType: 'json',
			data: {'valueindex': valueindex.toString(),'ids':ids.toString()},
			success:function(data){
                console.log(data);
                query_any();
			},
			error:function(){
				console.log('排序失败');
			}
		});  
	}
	
	function updateStatus(id,status){
		var url=ctx + "/loanactivity/updateStatus.action";
		 $.ajax({
			"url":url ,
			"type": "POST",
			"dataType": 'json',
			"data":  {"id":id,"status":status},
			"beforeSend": function(){
			},
			"success": function(data){
				alert("操作成功");
				query_any();
			},
			"error": function(){
				alert("操作失败");								 
			},
			"complete": function(){
				//解除事件
			}
		}); 
	}
	
	function doAddPro() {
		openDialogWithRand("/loanactivity/add.action",1000,1200,"新增活动",function (data){
			query_any();
		});
	}
	
	var check = true;
	function checkAll(){
		if(check){
			$(":checkbox").prop("checked", true);
			check=false;
		}else{
			$(":checkbox").prop("checked", false);
			check=true;
		}
	}
</script>
		
</body>
</html>